#pragma once
#include "knode.h"

class KList
{
public:
	KNode	m_ListHead;
	KNode	m_ListTail;

public:
	KList();
    void Clear();
	KNode*	GetHead();
	KNode*	GetTail();
	void AddHead(KNode *pNode);
	void AddTail(KNode *pNode);
	KNode*	RemoveHead();
	KNode*	RemoveTail();
	int		IsEmpty();
	long	GetNodeCount();
};

inline KList::KList()
{
	m_ListHead.m_pNext = &m_ListTail;
	m_ListTail.m_pPrev = &m_ListHead;
}

inline void KList::Clear() 
{
    m_ListHead.m_pNext = &m_ListTail;
    m_ListTail.m_pPrev = &m_ListHead;
}

inline int KList::IsEmpty()
{
	return (m_ListHead.GetNext() == NULL);
}

inline KNode* KList::GetHead()
{
	return m_ListHead.GetNext();
}

inline KNode* KList::GetTail()
{
	return m_ListTail.GetPrev();
}

inline void KList::AddHead(KNode *pNode)
{
	m_ListHead.InsertAfter(pNode);
}

inline void KList::AddTail(KNode *pNode)
{
	m_ListTail.InsertBefore(pNode);
}

inline KNode* KList::RemoveHead()
{
	KNode* pNode = m_ListHead.GetNext();
	if (pNode)
	{
		pNode->Remove();
	}
	return pNode;
}

inline KNode* KList::RemoveTail()
{
	KNode* pNode = m_ListTail.GetPrev();
	if (pNode)
	{
		pNode->Remove();
	}
	return pNode;
}

inline long KList::GetNodeCount()
{
	long nNode = 0;
	KNode* pNode = GetHead();
	while (pNode)
	{
		pNode = pNode->GetNext();
		nNode++;
	}
	return nNode;
}

